home *** CD-ROM | disk | FTP | other *** search
- {*********************************************************}
- {* AAVfyChk *}
- {* Copyright (c) Julian M Bucknall 1998 *}
- {* All rights reserved. *}
- {*********************************************************}
- {* Verifying credit card and ISBN check digits *}
- {*********************************************************}
-
- {Note: this unit is released as freeware. In other words, you are free
- to use this unit in your own applications, however I retain all
- copyright to the code. JMB}
-
- unit AAVfyChk;
-
- interface
-
- function ValidateCreditCardNumber(const aValue : string) : boolean;
- function ValidateISBN(const aValue : string) : boolean;
-
- implementation
-
- function IsDigit(Ch : char) : boolean;
- begin
- Result := Ch in ['0'..'9'];
- end;
-
- function IsDigitOrX(Ch : char) : boolean;
- begin
- Result := Ch in ['0'..'9', 'X'];
- end;
-
- function ValidateCreditCardNumber(const aValue : string) : boolean;
- var
- i : integer;
- Total : integer;
- Dbl : integer;
- Ch : char;
- IsOddPosn : boolean;
- begin
- if (aValue = '') then begin
- Result := false;
- Exit;
- end;
- Total := 0;
- IsOddPosn := true;
- for i := length(aValue) downto 1 do begin
- Ch := aValue[i];
- if IsDigit(Ch) then begin
- if IsOddPosn then
- inc(Total, ord(Ch) - ord('0'))
- else begin
- Dbl := (ord(Ch) - ord('0')) * 2;
- inc(Total, Dbl);
- if Dbl >= 10 then
- dec(Total, 9);
- end;
- IsOddPosn := not IsOddPosn;
- end;
- end;
- Result := (Total mod 10) = 0;
- end;
-
- function ValidateISBN(const aValue : string) : boolean;
- var
- i : integer;
- Total : integer;
- Multiplier : integer;
- FirstDigit : boolean;
- Ch : char;
- begin
- if (aValue = '') then begin
- Result := false;
- Exit;
- end;
- FirstDigit := true;
- for i := length(aValue) downto 1 do begin
- Ch := aValue[i];
- if FirstDigit then begin
- if IsDigitOrX(Ch) then begin
- if (Ch = 'X') then
- Total := 10
- else
- Total := (ord(Ch) - ord('0'));
- FirstDigit := false;
- Multiplier := 2;
- end
- end
- else {not the first digit} begin
- if IsDigit(Ch) then begin
- inc(Total, (ord(Ch) - ord('0')) * Multiplier);
- inc(Multiplier);
- if (Multiplier = 11) then
- Multiplier := 1;
- end;
- end;
- end;
- Result := (Total mod 11) = 0;
- end;
-
- end.
-